package leetcode;

public class C7004 {
        public static void main(String[] args) {
            char [][]grid=new char [][]{{'1','1','0','0','0'},
                    {'1','1','0','0','0'},
                    {'0','0','1','0','0'},
                    {'0','0','0','1','1'}};
            int i,j,num=0;
            for(i=0;i< grid.length;i++){
                for(j=0;j< grid[0].length;j++){
                    if (grid[i][j]=='1') {
                        num++;
                        dfs(grid,i,j);

                    }
                }
            }
            System.out.println(num);

        }

        private static void dfs(char[][] grid, int i, int j) {


            if(i>0&&i< grid.length&&grid[i-1][j]=='1'){
                grid[i-1][j]='0';
                dfs(grid,i-1,j);
            } if(i>=0&&i< grid.length-1&&grid[i+1][j]=='1') {
                grid[i + 1][j] = '0';
                dfs(grid, i + 1, j);
            }if(j>=0&&j< grid[0].length-1&&grid[i][j+1]=='1') {
                grid[i][j+1] = '0';
                dfs(grid, i , j+1);
            }if(j>0&&j< grid[0].length&&grid[i][j-1]=='1') {
                grid[i ][j-1] = '0';
                dfs(grid, i , j-1);
            }
        }



}
